//观看记录
import AJAX from '@/utils/ajax'
import User from '@/components/user'
import { isLogin } from '@/utils'

class PlayLog {
  constructor() {
    this.user = new User()
    this.get()
    this.add()
  }

  del(id) {
    if (isLogin()) {
      AJAX({ url: 'delPlaylog', data: { log_id: id } }).then(res => {
        if (res.code > 0) {
          this.get()
        }
        layer.msg(res.msg)
      })
    } else {
      const playlog = JSON.parse(localStorage.getItem('playlog') || '[]')
      playlog.splice(id, 1)
      localStorage.setItem('playlog', JSON.stringify(playlog))
      this.get()
    }
  }

  clear() {
    if (isLogin()) {
      AJAX({ url: 'clearPlaylog' }).then(res => {
        if (res.code > 0) {
          this.get()
        }
        layer.msg(res.msg)
      })
    } else {
      localStorage.removeItem('playlog')
    }
  }

  add() {
    const playlog = $('.playlog-set')
    if (playlog.eq(0).attr('data-pid')) {
      let data = {
        log_vid: playlog.data('id'),
        log_sid: playlog.data('sid'),
        log_pid: playlog.data('pid'),
        log_urlname: playlog.data('name'),
        log_maxnum: playlog.data('count')
      }
      if (isLogin()) {
        AJAX({ url: 'setPlaylog', method: 'post', data })
      } else {
        data = Object.assign({}, data, { log_title: playlog.data('title') })
      }

      let dataList = JSON.parse(localStorage.getItem('playlog') || '[]')
      for (var i in dataList) {
        const d = JSON.parse(dataList[i])
        if (d.log_vid === data.log_vid) {
          dataList.splice(i, 1)
        }
      }
      delete data._t
      dataList.unshift(JSON.stringify(data))
      if (dataList.length > 10) {
        dataList.pop()
      }
      localStorage.setItem('playlog', JSON.stringify([...new Set([...dataList])]))
    }
  }

  close() {
    $('.playlog-list').hide()
  }

  on() {
    const that = this
    $('.playlog-clear').click(function() {
      that.clear()
    })
    $('.playlog-close').click(function() {
      that.close()
    })
    $('.playlog-del').click(function() {
      that.del($(this).data('id'))
    })
  }

  get() {
    let innerStr = ''
    if (isLogin()) {
      AJAX({ url: 'getPlaylog' }).then(res => {
        $('.playlog-list').html(res)
        this.on()
        this.user.userinfo()
      })
    } else {
      const playlog = JSON.parse(localStorage.getItem('playlog') || '[]')
      if (playlog.length > 0) {
        innerStr =
          '<div class="playlog-tool"><a class="playlog-clear" href="javascript:;" target="_self">清空全部播放记录</a><a class="playlog-close" href="javascript:;" target="_self">关闭</a></div><ul>'
        for (var i in playlog) {
          const d = JSON.parse(playlog[i])
          const { log_vid, log_sid, log_pid, log_urlname, log_maxnum, log_title } = d
          const url = `/ac/${log_vid}/`
          const playUrl = `/ac/${log_vid}/${log_sid}-${log_pid}.html`
          const nextUrl = log_pid < log_maxnum ? `<a href="/ac/${log_vid}/${log_sid}-${log_pid - 1}.html">下一集</a>` : ''
          innerStr += `<li><span><a href="${url}">${log_title}</a><a href="${playUrl}">${log_urlname}</a></span><a href="${playUrl}">续播</a>${nextUrl}<a href="javascript:;" class="playlog-del" data-id="${i}" data-vid="${log_vid}"><i class="icon iconfont"></i></a></li>`
        }
        innerStr += '</ul><div class="playlog-todo" id="playlog-todo"><a id="loginbarx" href="javascript:;">登录</a>后将能永久保存播放记录</div>'
      } else {
        innerStr = '<ul><strong>暂无观看历史记录列表</strong></ul>'
      }
      $('.playlog-list').html(innerStr)
      this.on()
    }
  }
}

export default PlayLog
